package content
{
	import _Pan3D.base.Camera3D;
	import _Pan3D.base.Focus3D;
	import _Pan3D.base.Object3D;
	import _Pan3D.core.Groundposition;
	import _Pan3D.core.Mathclass;
	
	import _me.Scene_data;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.utils.getTimer;
	
	public class NewKeyControl extends EventDispatcher
	{
		private var _stage:Stage;
		private var _isFrist:Boolean=true;
		private var _keyobj:Object=new Object;

		public function NewKeyControl()
		{
		}

		public function init(temp_stage:Stage):void
		{
			_stage=temp_stage;
			_stage.addEventListener("keyDown", keydownHandler);
			_stage.addEventListener("keyUp", keyupHandler);

			_stage.addEventListener(MouseEvent.MOUSE_DOWN, stageMouseDown);
			_stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);
			_stage.addEventListener(MouseEvent.RIGHT_MOUSE_DOWN,onRightMouseDown);
		}
		private function stageMouseDown(event:MouseEvent):void
		{
			
				
		}
		/*
		private function getNearbyPoint(beginX:Number, beginZ:Number, endX:Number, endZ:Number):Point{
			var angly:Number = Mathclass.math_angle(endX, endX, beginX, beginZ)
			var distance:Number = 3;
			endX -= distance * Math.cos(angly * Math.PI / 180);
			endZ -= distance * Math.sin(angly * Math.PI / 180);
			return new Point(endX,endZ);
		}*/
		
		private var seepNum:Number=3;


		private function mouseWheel(evt:MouseEvent):void
		{
			Scene_data.cam3D.distance=Scene_data.cam3D.distance + evt.delta * 1;
			
			//Scene_data.cam3D.angle_y=Scene_data.cam3D.angle_y + evt.delta * 10;
			//Scene_data.focus3D.angle_y = Scene_data.focus3D.angle_y + evt.delta * 10;
			Scene_data.focus3D.angle_x = -45*(Scene_data.cam3D.distance/90); 
			//trace(Scene_data.cam3D.distance)
		}

		private function keydownHandler(evt:KeyboardEvent):void
		{
			_keyobj[evt.keyCode]=true;
			_isFrist=false;
			/*if (evt.keyCode == 87)
			{
				this.dispatchEvent(evt);
			} else if(evt.keyCode == 32){
				Scene_data.self3DMove.attack("attack");
			} else if(evt.keyCode == 65){
				Scene_data.self3DMove.attack("attack2");
			}
*/
		}

		private function keyupHandler(evt:KeyboardEvent):void
		{
			delete _keyobj[evt.keyCode];
			if (evt.keyCode == 87)
			{
				this.dispatchEvent(evt);
			}

		}

		public function isDown(key:Number):Boolean
		{
			return _keyobj[key] ? true : false;
		}
		
		private var downMouseX:int;
		public function onRightMouseDown(event:MouseEvent):void{
			downMouseX = _stage.mouseX;
			_stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			_stage.addEventListener(MouseEvent.RIGHT_MOUSE_UP,onRightMouseUp);
		}
		private function onMouseMove(event:MouseEvent):void{
			var delta:int = _stage.mouseX - downMouseX;
			Scene_data.focus3D.angle_y = Scene_data.focus3D.angle_y - delta;
			downMouseX = _stage.mouseX;
		}
		private function onRightMouseUp(event:MouseEvent):void{
			_stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			_stage.removeEventListener(MouseEvent.RIGHT_MOUSE_UP,onRightMouseUp);
		}
	}
}
